﻿using System.IO;
using BSDS.Core.Data.Local;

namespace BSDS.Core.Model.Services.Uploading.DataProviders
{
    public class DataProviderBase
    {
        private const int ChunkSize = 200000;

        protected MemoryStream MemoryStream { get; private set; }

        public long DataSizeInBytes { get { return MemoryStream.Length; } }

        public DataFormat Format { get; private set; }

        public DataProviderBase(DataFormat format)
        {
            Format = format;

            MemoryStream = new MemoryStream();
        }

        public void WriteTo(Stream stream)
        {
            MemoryStream.Seek(0, SeekOrigin.Begin);

            var buffer = new byte[ChunkSize];

            int bytesRead = MemoryStream.Read(buffer, 0, buffer.Length);
            while (bytesRead > 0)
            {
                stream.Write(buffer, 0, bytesRead);

                bytesRead = MemoryStream.Read(buffer, 0, buffer.Length);
            }
        }

        public void Dispose()
        {
            if(MemoryStream != null)
            {
                MemoryStream.Dispose();
            }
        }
    }
}